<template>
  <div class="login-container">
    <el-card class="login-card">
      <h2 class="login-title">学生登录</h2>
      <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="rules"
          label-width="100px"
          class="login-form"
      >
      <el-form-item label="学号" prop="studentId">
          <el-input
              v-model="loginForm.studentId"
              autocomplete="off"
              placeholder="请输入6位学号">
          </el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
              type="password"
              v-model="loginForm.password"
              autocomplete="off"
              placeholder="请输入密码">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login" class="login-button">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import axios from "axios";

export default {
  data() {
    return {
      loginForm: {
        studentId: "",
        password: ""
      },
      rules: {
        studentId: [
          { required: true, message: "请输入学号", trigger: "blur" },
          { pattern: /^\d{6}$/, message: "学号必须是6位数字", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            pattern: /^[A-Za-z0-9]{6,}$/,
            message: "密码必须包含数字和字母，且长度不小于6位，不能包含特殊字符",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    async login() {
      this.$refs.loginForm.validate(async (valid) => {
        if (!valid) {
          this.$message.error("请修正表单中的错误后重试");
          return;
        }
        try {
          const response = await axios.post("http://localhost:3000/login", this.loginForm);
          const { data } = response;
          this.$message.success(data.message || "登录成功");
          if (data.token) {
            localStorage.setItem("token", data.token);
            this.$router.push("/studentPage/studentHome");
          } else {
            this.$message.warning("登录成功但未收到Token");
          }
        } catch (err) {
          let errorMessage = "登录失败";
          if (err.response && err.response.data) {
            errorMessage = err.response.data.message || errorMessage;
          }
          this.$message.error(errorMessage);
        }
      });
    }

  }
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #42a5f5, #7e57c2);
}

.login-card {
  width: 360px;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
  background-color: #ffffff;
  padding: 20px;
}

.login-title {
  text-align: center;
  font-size: 24px;
  font-weight: bold;
  color: #333333;
  margin-bottom: 20px;
}

.login-form {
  padding: 10px 0;
}

.el-form-item {
  margin-bottom: 20px;
}

.el-input {
  border-radius: 8px;
  height: 40px;
}

.login-button {
  margin: 20px auto;

  width: 100%;
  height: 45px;
  border-radius: 22px;
  font-size: 16px;
  font-weight: bold;
  background: linear-gradient(90deg, #42a5f5, #7e57c2);
  color: #ffffff;
  border: none;
  transition: all 0.3s;
}

.login-button:hover {
  background: linear-gradient(90deg, #7e57c2, #42a5f5);
  transform: scale(1.05);
}
</style>
